/*******************    (C) COPYRIGHT SUNSHINE SILICON    **********************
* File Name: main.c
* Description:

    HS6200 Demo
      
*  Copyright (C) 2014-2035 Sunshine Silicon Corporation (www.sunsili.com), All Rights Reserved
*  SPDX-License-Identifier: Apache-2.0
*
*  Licensed under the Apache License, Version 2.0 (the "License"); you may
*  not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
*  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.

 *  #define SPI_CE      GPIOA, GPIO_PIN_2       
    #define SPI_CS      GPIOA, GPIO_PIN_3
    #define SPI_CLK     GPIOC, GPIO_PIN_5
    #define SPI_MOSI    GPIOC, GPIO_PIN_6
    #define SPI_MISO    GPIOC, GPIO_PIN_7
    #define SPI_IRQ     GPIOA, GPIO_PIN_1                                                                                                                              
 *                                                                                                                                  
 *                                                                                                                                  
 *                                    ___________________________                           \ /    
 *                                   |                           |        _____________      |                             
 *                                   |                           |       |             |     |                                  
 *                                   |                     PA2   | --->  |  nCE        |-----|                                     
 *                                   |                S  | PC5   | --->  |  SCK | S    |   H                                     
 *                                   |                P  | PC7   | <---  |  MISO| P    |   S                                     
 *                                   |                I  | PC6   | --->  |  MOSI| I    |   6  (as PRX)                     
 *                                   |                   | PA3   | --->  |  nSS |      |   2                                     
 *             /-------------------- | VBUS                PA1   | <---  |  IRQ        |   0                                     
 *      P ____/RX________USB to_____ | TX                        |       |_____________|   0                                     
 *      C     \RX        UART        | RX                        |                                               
 *             \-------------------- | GND                       |                                         
 *                                   |                           |                                     
 *                                   |                           |                                                     
 *                                   |                           |                                           
 *                                   |                           |                                                              
 *                                   |                     PD2   | ---> SYSCLK Output,used for test.                            
 *                                   |                           |                                                                  
 *                                   |                           |                                                               
 *                                   |                     PD3   | ---> LED1 ( LED On when P2.2 output 0 )                       
 *                                   |                     PD4   | ---> LED2 ( LED On when P2.3 output 0 )                       
 *                                   |                           |                                                               
 *                                   |___________________________|                                                                   
 *                                                                                                                                 
 *                                             STM8S003F3                                                                                        
 *                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
 *  
* Version    Date       Auther      Reversed History
  ----------------------------------------------------------------------------
  V1.0.0    2021-10-20  Lojam Fan  Fisrt Created 

*******************************************************************************/

#include "stm8s.h"
#include "TIM.h"
#include "stm8s_uart1.h"
#include "HS6200.h"
#include "softDelay.h"
//#include <stdio.h>
//#include <stdlib.h>



#define APP_LOG_ENABLE     0
#if APP_LOG_ENABLE 
#define APP_LOG_DEBUG(...)       uart_printf(__VA_ARGS__)
#else 
#define APP_LOG_DEBUG(...) 
#endif




u8 TXBuffer[2] = {0xAA, 0xAA};
u8 rx_buf[16];	

void sysClkInit(void)
{
    CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);
    CLK_HSICmd(ENABLE);
}


void uartInit(void)
{    
  UART1_DeInit();
  /* UART1 configuration ------------------------------------------------------*/
  /* UART1 configured as follow:
        - BaudRate = 9600 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Receive and transmit enabled
        - UART1 Clock disabled
  */
    UART1_Init((uint32_t)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
              UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
    UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);   
    UART1_Cmd(ENABLE);
} 

//STM8S,printf函数重定向
char putchar(char c)
{
    if (c == '\n')
    {
      while (!(UART1->SR & 0x40));
      UART1->DR = ('\r');
      while (!(UART1->SR & 0x40)); //等待发送完成
    }

    while (!(UART1->SR & 0x80));
    UART1->DR = (c);
    while (!(UART1->SR & 0x80)); //等待发送完成

    return (c);
}


void uart_printf(unsigned char *buffer)
{
	while (*buffer != '\0')
	{
	    putchar(*buffer++);
    }
}


void timInit(void)
{
    TIM1_Cmd(ENABLE);
    TIM2_Cmd(ENABLE); 
}


#define GPIO_LED_RED    GPIOD, GPIO_PIN_2 
#define GPIO_LED_GRN    GPIOD, GPIO_PIN_3 
#define GPIO_LED_TEST   GPIOD, GPIO_PIN_4 
#define LED_RED_ON()    clrGPIO(GPIO_LED_RED)
#define LED_GRN_ON()    clrGPIO(GPIO_LED_GRN)
#define LED_TEST_ON()   clrGPIO(GPIO_LED_TEST)
#define LED_RED_OFF()    setGPIO(GPIO_LED_RED)
#define LED_GRN_OFF()    setGPIO(GPIO_LED_GRN)
#define LED_TEST_OFF()   setGPIO(GPIO_LED_TEST)


void led_init(void)
{
    GPIO_Init(GPIO_LED_RED,  GPIO_MODE_OUT_PP_HIGH_FAST);
    GPIO_Init(GPIO_LED_GRN,  GPIO_MODE_OUT_PP_HIGH_FAST);
    GPIO_Init(GPIO_LED_TEST,  GPIO_MODE_OUT_PP_HIGH_FAST);
}


int main( void )
{
    u8 recvLen = 0;
    
    sysClkInit();
    uartInit(); 
    
#ifdef USE_SOFT_SPI
    soft_spi_init();
 #endif
    
//    led_init();
    UART1_SendData8(0x00);
    APP_LOG_DEBUG("HS6200 Init ... \n");
    HS6200_Init();
    UART1_SendData8(0x01);
    APP_LOG_DEBUG("HS6200 Init Sucesss! \n");
    
//enableInterrupts();

    while(1)
    { 
        APP_LOG_DEBUG("HS6200 Send...AA AA \n");
        HS6200_ModeSwitch(Rf_PTX_Mode);
        HS6200_Clear_All_Irq();
        HS6200_Flush_Tx();
        HS6200_SendPack(HS6200_W_TX_PAYLOAD , TXBuffer, 2);
        
        Delay_ms(10);
         
        HS6200_ModeSwitch(Rf_PRX_Mode);
        HS6200_Clear_All_Irq();
        HS6200_Flush_Rx();
        u8 i = 100;
        while(i--)
        {
            Delay_ms(1);
            recvLen = HS6200_RecivePack(rx_buf);
                
            if(	recvLen )
            {                              
                HS6200_Clear_All_Irq();
                HS6200_Flush_Rx();         
                APP_LOG_DEBUG("HS6200 Recv \n");
                for(u8 i=0; i<recvLen; i++)
                {                
                    UART1_SendData8(rx_buf[i]);  
                    while(UART1_GetFlagStatus(UART1_FLAG_TXE)!=SET);                    
                } 
                break;
            }         
        }
        UART1_SendData8(i); 
        Delay_ms(100);
    } 
}


/******************* (C) COPYRIGHT SUNSHINE SILICON  **************************
********************        END OF FILE main.c        *******************/
